package com.gitee.wsl.base

import com.gitee.wsl.ext.base.Compare
import com.gitee.wsl.ext.base.ifValue

object Objects {
    fun hashCode(vararg obs:Any?) = obs.contentHashCode()

    fun hash(vararg obs:Any?) = hashCode(* obs )

    fun toStringHelper(o:Any) = ToStringBuilder(o)

    fun <T> equals(a:T,b:T) = Compare.equals(a,b)

    fun<T> requireNonNull(value: T?, string: String):T = value ?: throw NullPointerException(string)

    fun <V> compare(v1: V?, v2: V?): Int {
        return  if (v1 == null)
            (v2 == null).ifValue(0,-1)
        else if(v2 == null)
            1
        else if(v1::class is Comparable<*>)
            compareValues(v1 as Comparable<*>,v2 as Comparable<*>)
        else
            0
    }
}

class ToStringBuilder(val o:Any,val base: StringBuilder = StringBuilder()): Appendable by base, CharSequence by base{

    fun add(name: Any, value: Any):ToStringBuilder{
        appendLine("$name:$value")
        return this
    }

    override fun toString() = "class: ${o::class.simpleName} {\n ${base.toString()} \n}\n"
}
